package adger.me.yili.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;

import adger.me.yili.R;
import adger.me.yili.view.impl.OrderDetailActivity;

/**
 * Created by adger on 16/1/20.
 */
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderViewHolder> {
    private LayoutInflater mInflater;
    private Context mContext;
    private JSONArray mDatas;


    public OrderAdapter(Context context, JSONArray arrays) {
        this.mContext = context;
        this.mDatas = arrays;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item, parent, false);
        OrderViewHolder viewHolder = new OrderViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(OrderViewHolder holder, final int position) {

        try {
            holder.username.setText(mDatas.getJSONObject(position).getString("user"));
            String charege = String.format("%.2f", Float.parseFloat(mDatas.getJSONObject(position).getString("charge")));
            if (Float.parseFloat(charege) == 0){
                holder.money.setText("未知");
            }else {
                holder.money.setText(charege);
            }
            holder.count.setText(mDatas.getJSONObject(position).getString("group_no"));
            holder.find.setText("查询");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        // 查询
        holder.find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, OrderDetailActivity.class);
                Bundle bundle = new Bundle();

                try {
                    bundle.putString("order_id", String.valueOf(mDatas.getJSONObject(position).getString("id")));
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.length();
    }

    class OrderViewHolder extends ViewHolder {
        public TextView username;
        public TextView money;
        public TextView count;
        public TextView find;

        public OrderViewHolder(View itemView) {
            super(itemView);
            username = (TextView) itemView.findViewById(R.id.username_item);
            money = (TextView) itemView.findViewById(R.id.money_item);
            count = (TextView) itemView.findViewById(R.id.count_item);
            find = (TextView) itemView.findViewById(R.id.find_item);
        }
    }
}


